package controller;

import java.awt.Color;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.image.BufferedImage;

import javax.swing.BorderFactory;
import javax.swing.JFileChooser;
import javax.swing.border.EtchedBorder;

import model.Database;
import model.IOFile;
import model.OptionMessenger;
import model.UICommand;
import view.ConfigurePanel;
import view.LogginPanel;

import com.alee.extended.image.WebImageDrop;

public class ConfigureController implements ActionListener,
		MouseMotionListener, MouseListener {
	private ConfigurePanel view;

	public ConfigureController(LogginPanel component) {
		this.view = new ConfigurePanel(component, this);
		// FIXME
		// view.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
		// view.setVisible(true);
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		String cmd = e.getActionCommand();
		switch (cmd) {
		case "check":
			view.checkConnection();
			break;
		case "save":
			view.saveConnection();
			break;
		case "default":
			int answer = OptionMessenger
					.showQuestionDilog(
							view,
							"Tất cả thông tin cấu hình sẽ được phục hồi về mặc định\nTiếp tục thực hiện?",
							"Cảnh báo", OptionMessenger.WARNING_MESSAGE,
							OptionMessenger.OK_OPTION);
			if (answer == OptionMessenger.OK_OPTION)
				restore();
			break;
		case UICommand.BACK_CMD:
			view.setVisible(false);
			if (view.isShowingConfig())
				view.getInforDatabase();
			break;
		case UICommand.SHOW_CMD:
			new JFileChooser().showOpenDialog(view);
			break;
		}
	}

	private void restore() {
		try {
			Database.restoreDatabase();
			view.setDatabase(Database.newInstance());
			view.getInforDatabase();
		} catch (ClassNotFoundException e) {
			e.printStackTrace(IOFile.getInstance());
		}
	}

	public void show() {
		this.view.setVisible(true);
	}

	@Override
	public void mouseDragged(MouseEvent e) {
		// TODO Auto-generated method stub
	}

	@Override
	public void mouseMoved(MouseEvent e) {
		((WebImageDrop) e.getSource()).setBorder(BorderFactory
				.createEtchedBorder(EtchedBorder.RAISED));
		((WebImageDrop) e.getSource()).setBackground(Color.blue);
	}

	@Override
	public void mouseClicked(MouseEvent e) {
		if (e.getClickCount() > 1) {
			WebImageDrop web = (WebImageDrop) e.getSource();
			BufferedImage img = web.getImage();
			Image image = null;
			if (img != null) {
				int scaleX = img.getWidth();
				int scaleY = img.getHeight();
				image = img.getScaledInstance(scaleX, scaleY,
						Image.SCALE_DEFAULT);
			}
			view.setImg(image);
			view.changeBackground();
		}
	}

	@Override
	public void mousePressed(MouseEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public void mouseReleased(MouseEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public void mouseEntered(MouseEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public void mouseExited(MouseEvent e) {
		((WebImageDrop) e.getSource()).setBorder(null);
		((WebImageDrop) e.getSource()).setBackground(null);
	}

}
